﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Data;
using System.IO;
using NPOI.HSSF.UserModel;
using NPOI.HSSF.Util;
using NPOI.HPSF;
using Facade;
using Facade.Excel;

namespace PeisongWeb.admin
{
    public partial class companyGPMAnalysisList : adminPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (hasPermission("EH", 1))
                {
                    rptBind();
                }
                else
                {
                    Library.Script.ClientMsgUrl("没有权限！", "index.aspx");
                    return;
                }
            }
        }

        private void rptBind()
        {
            StringBuilder where = new StringBuilder();
            if (!string.IsNullOrEmpty(txtCompanyName.Text))
                where.AppendFormat(" AND t.[company] LIKE '%{0}%'", txtCompanyName.Text);

            int year = DateTime.Now.Year;
            int.TryParse(ddlYear.SelectedValue, out year);
            int month = 0;
            int.TryParse(ddlMonth.SelectedValue, out month);

            string orderByColumn = "t.companyId ASC";

            if (ViewState["orderByColumn"] != null)
            {
                orderByColumn = ViewState["orderByColumn"].ToString();
            }

            hdCurrentOrderBYColumn.Value = orderByColumn;

            DataSet ds = logic.company.listCompanyGPMAnalysisList(year, month, where.ToString(), orderByColumn, AspNetPager1.PageSize, AspNetPager1.CurrentPageIndex);
            AspNetPager1.RecordCount = Convert.ToInt32(ds.Tables[0].Rows[0][0]);
            rpt_list.DataSource = ds.Tables[1];
            rpt_list.DataBind();

            rpt_rollup_list.DataSource = ds.Tables[2];
            rpt_rollup_list.DataBind();
        }

        protected void link_Search_Click(object sender, EventArgs e)
        {
            AspNetPager1.CurrentPageIndex = 1;
            rptBind();
        }

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            rptBind();
        }

        protected void link_Order_Comand(object sender, CommandEventArgs e)
        {
            if (e.CommandName != null)
            {
                string orderByDirect = hdCurrentOrderDirect.Value == "ASC" ? "DESC" : "ASC";
                hdCurrentOrderDirect.Value = orderByDirect;

                ViewState["orderByColumn"] = e.CommandName.ToString() + string.Format(" {0}", orderByDirect);
                AspNetPager1.CurrentPageIndex = 1;
                rptBind();
            }
        }

        protected void link_ExportToExcel_Click(object sender, EventArgs e)
        {
            int year = DateTime.Now.Year;
            int.TryParse(ddlYear.SelectedValue, out year);
            int month = 0;
            int.TryParse(ddlMonth.SelectedValue, out month);
            string orderByColumn = "t.companyId";

            if (ViewState["orderByColumn"] != null)
            {
                orderByColumn = ViewState["orderByColumn"].ToString();
            }

            DataTable dt = logic.company.exportCompanyGPMAnalysisToExcel(year, month, orderByColumn);
            dt.Columns[0].ColumnName = "企业名称";
            dt.Columns[1].ColumnName = "总进价";
            dt.Columns[2].ColumnName = "总出价";
            dt.Columns[3].ColumnName = "毛利";
            dt.Columns[4].ColumnName = "毛利率";

            string title = string.Format("采购配送{0}年{1}企业毛利分析", ddlYear.SelectedValue, (month != 0 ? month + "月" : ""));

            // 导出Excel
            ExportFacade exportInstance = new Facade.Excel.ExportFacade();
            Dictionary<int, FormatStyle> formatOptions = new Dictionary<int, FormatStyle>();
            formatOptions.Add(1, FormatStyle.ToFix2);
            formatOptions.Add(2, FormatStyle.ToFix2);
            formatOptions.Add(3, FormatStyle.ToFix2);
            formatOptions.Add(4, FormatStyle.Percent);

            List<int> footerAvgColumns = new List<int>()
            {
                1,2,3,4
            };
            List<int> footerSumColumns = new List<int>()
            {
                1,2,3
            };

            exportInstance.ExportByWeb(dt, title, title + ".xls", formatOptions, "企业毛利率分析", footerAvgColumns, footerSumColumns);
        }
    }
}
